#!/bin/rc
# Tries to determine what type of file you are printing and do the correct
# thing with it.
# It currently knows about images, troff intermediate, and ascii files.
rfork e
temp=/tmp/lp$pid
fn sigexit { rm -f $temp }
proc=$LPLIB/process

if (! ~ $DEBUG '')
	flag x +
cat >$temp

type=`{file $temp}
switch ($type(2)) {
case troff
	switch ($LPCLASS) {
	case *Latin1* *post* *opost*
		switch ($type(5)) {
		# Latin1 is for compatibility with old research UNIX systems,
		# doesn't work on Plan 9
		case Latin1 post
			tcs -s -f utf -t latin1 <$temp | $proc/dpost
		case UTF
			$proc/tr2post <$temp
		}
	case *gs!* *gsijs!*
		switch ($type(5)) {
		# Latin1 is for compatibility with old research UNIX systems,
		# doesn't work on Plan 9
		case Latin1 post
			tcs -s -f utf -t latin1 <$temp | $proc/dpost |
				$proc/gspipe
		case UTF
			$proc/tr2post <$temp | $proc/gspipe
		}
	case *
		echo $type(2) -T$type(5) output is improper for $LPDEST >[1=2]
	}
case special
	switch ($type(4)) {
	case '#b'
		switch ($LPCLASS) {
		case *post*
			$proc/p9bitpost <$temp
		case *gs!*
			$proc/p9bitpost <$temp | $proc/gspipe
		case *gsijs!*
			$proc/p9bitpost <$temp | $proc/gspipeijs
		}
	case *
		echo $type file is improper for $LPDEST >[1=2]
	}
case Compressed plan old subfont
	# type is really 'Compressed image' or 'plan 9 image' or
	# 'old plan 9 image'
	# 'subfont' is to cope with a bug in png (writing wrong length),
	# 6 may 2008
	switch ($LPCLASS) {
	case *post*
		$proc/p9bitpost <$temp
	case *gs!*
		$proc/p9bitpost <$temp | $proc/gspipe
	case *gsijs!*
		$proc/p9bitpost <$temp | $proc/gspipeijs
	}
case jpeg
	switch ($LPCLASS) {
	case *post*
		$proc/jpgpost <$temp
	case *gs!*
		$proc/jpgpost <$temp | $proc/gspipe
	case *gsijs!*
		$proc/jpgpost <$temp | $proc/gspipeijs
	}
case GIF
	switch ($LPCLASS) {
	case *post*
		$proc/gifpost <$temp
	case *gs!*
		$proc/gifpost <$temp | $proc/gspipe
	case *gsijs!*
		$proc/gifpost <$temp | $proc/gspipeijs
	}
case PNG
	switch ($LPCLASS) {
	case *post*
		$proc/pngpost <$temp
	case *gs!*
		$proc/pngpost <$temp | $proc/gspipe
	case *gsijs!*
		$proc/pngpost <$temp | $proc/gspipeijs
	}
case ccitt-g31
	switch ($LPCLASS) {
	case *post*
		$proc/g3post <$temp
	case *gs!*
		$proc/g3post <$temp | $proc/gspipe
	case *gsijs!*
		$proc/g3post <$temp | $proc/gspipeijs
	}
case bitmap
	# bitmap for research UNIX compatibility, does not work on Plan 9.
	switch ($LPCLASS) {
	case *post*
		$proc/bpost <$temp
	case *mhcc*
		$proc/bpost <$temp | $proc/mhcc
	case *
		echo $type(2) file is improper for $LPDEST >[1=2]
	}
case tex
	mv $temp $temp.dvi
	temp=$temp.dvi
	switch ($LPCLASS) {
	case *post*
		$proc/dvipost $temp
	case *gs!*
		$proc/dvipost $temp | $proc/gspipe
	case *gsijs!*
		$proc/dvipost $temp | $proc/gspipeijs
	case *
		echo $type(2) file is improper for $LPDEST >[1=2]
	}
case postscript
	switch ($LPCLASS) {
	case *post*
		$proc/post <$temp
	case *gs!*
		$proc/post <$temp | $proc/gspipe
	case *gsijs!*
		$proc/post <$temp | $proc/gspipeijs
	case *
		echo $type(2) file is improper for $LPDEST >[1=2]
	}
case HPJCL HP
	switch ($LPCLASS) {
	case *HPJCL*
		$proc/noproc <$temp
	case *
		echo $type(2) file is improper for $LPDEST >[1=2]
	}
case daisy
	switch ($LPDEST) {
	case *
		echo $type(2) file is improper for $LPDEST >[1=2]
	}
case tiff
	switch ($LPCLASS) {
	case *post*
		$proc/tiffpost $temp
	case *gs!*
		$proc/tiffpost $temp | $proc/gspipe
	case *gsijs!*
		$proc/tiffpost $temp | $proc/gspipeijs
	case *
		echo Unrecognized class of line printer for $LPDEST >[1=2]
	}
case PDF
	switch ($LPCLASS) {
	case *pdf*
#		if (~ $LPCLASS *duplex* && ~ $LPCLASS *HPJCL*)
#			echo -n '&l1S'		# HP JCL: duplex on
		if (~ $LPCLASS *duplex* && ~ $LPCLASS *HPJCL*) {
			echo '%-12345X@PJL DEFAULT DUPLEX=ON'	# HP PJL
			echo '%-12345X'
			echo '%-12345X@PJL DEFAULT PS:MBT=ON'	# `memory boost'
			echo '%-12345X'
		}
		cat $temp		# pass pdf unaltered to pdf printer
	case *post*
		$proc/pdfpost $temp
	case *gs!*
		$proc/pdfgs $temp
	case *gsijs!*
		$proc/pdfgsijs $temp
	case *
		echo Unrecognized class of line printer for $LPDEST >[1=2]
	}
case microsoft				# office document
	switch ($LPCLASS) {
	case *post*
		doc2ps $temp | $proc/post
	case *gs!*
		doc2ps $temp | $proc/post  | $proc/gspipe
	case *gsijs!*
		doc2ps $temp | $proc/post | $proc/gspipeijs
	case *
		echo $type(2) file is improper for $LPDEST >[1=2]
	}
case empty
	echo file is empty >[1=2]
case cannot
	echo cannot open file >[1=2]
case English Cyrillic short extended alef limbo [Aa]scii assembler c latin rc sh \
    as mail email message/rfc822 manual HTML
	switch ($LPCLASS) {
	case *post*
		$proc/ppost <$temp
	case *gs!*
		$proc/ppost <$temp | $proc/gspipe
	case *gsijs!*
		$proc/ppost <$temp | $proc/gspipeijs
	case *canon*
		$proc/can $* <$temp
	case *
		echo Unrecognized class of line printer for $LPDEST >[1=2]
	}
case *
	echo $type(2) file is improper for $LPDEST >[1=2]
}

wait
rv=$status
rm -f $temp
# exit $rv
exit
